package com.deyuanyun.pic.settings.support.biz;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.domain.base.Code;
import com.deyuanyun.pic.settings.domain.base.UnifiedEncoding;
import com.deyuanyun.pic.settings.mapper.base.UnifiedEncodingMapper;
import com.deyuanyun.pic.settings.service.base.AttachmentService;
import com.deyuanyun.pic.settings.service.base.CodeService;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 附件查询
 *
 * @author huanglw
 * @date 2016-6-30
 *
 * History: [修改人][时间][修改描述]
 */
public class AttachmentUtil {

    static Logger logger= Logger.getLogger(AttachmentUtil.class);

    private static ListMultimap<String, Attachment> attachmentListMultimap= ArrayListMultimap.create();
    private static final String CODE_DICT_IN_CACHE="APPLICATION.CODE_DICT_IN_CACHE";

    private static AttachmentService attachmentService = SpringAppliction.getBean(AttachmentService.class);

    /**
     * 得到所有的 码表数据项
     */
    private static void loadAttachment(String businessId){
        logger.info("-------->加载图片");
        List<Attachment> list = attachmentService.queryAttachment(businessId);
        attachmentListMultimap.clear();
        TempStoreDataSupport.removeData(CODE_DICT_IN_CACHE);
        for (Attachment attachment : list){
            attachmentListMultimap.put(attachment.getBusinessId(), attachment);
        }
        TempStoreDataSupport.pushDataf(CODE_DICT_IN_CACHE, attachmentListMultimap);
    }

    /**
     * 根据业务编号，得到对应附件信息
     *
     * @param businessId
     * @return
     */
    public static List<Attachment> queryAttachmentList(String businessId){
        loadAttachment(businessId);
        return attachmentListMultimap.get(businessId);
    }

    /**
     * 根据业务编号，得到对应附件信息
     *
     * @param businessId
     * @return
     */
    public static List<Attachment> getAttachmentList(String businessId){
        return attachmentService.queryAttachment(businessId);
    }

    /**
     * 通过通过附件ID，更新业务ID
     *
     * @param businessId
     * @param id
     * @return
     */
    public static int updateBusinessId(String businessId, String id) {
        return attachmentService.updateBusinessIdById(businessId, id);
    }
}
